package com.project1;


import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class App {


    public static void main(String[] args) throws InvocationTargetException, InstantiationException, IllegalAccessException {
        //创建一个集合，用于存放用户选择的方法
        List<Integer> zhuanzhang=new ArrayList<>();
        Collections.addAll(   zhuanzhang, 2,3);


        trackerOptions(  zhuanzhang, Order.class  );

        System.out.println("========================");

        List<Integer> xiaodang=new ArrayList<>();
        Collections.addAll(   xiaodang, 0,1,2,3,4);


        trackerOptions(  xiaodang, Order.class  );


        System.out.println("========================");

        List<Integer> qingkucun=new ArrayList<>();
        Collections.addAll(   qingkucun, 1);


        trackerOptions(  qingkucun, Order.class  );
    }


    private static void trackerOptions(List<Integer> list, Class<Order> opClass) throws InstantiationException, IllegalAccessException, InvocationTargetException {
        Object o=opClass.newInstance();
        //利用反射取出这个  opClass中所有的方法
        Method[] ms= opClass.getDeclaredMethods();   //不包括继承的方法
        for(   int ordermethodid: list) {
            for (Method m : ms) {
                Step annotion = m.getAnnotation(Step.class);
                if (annotion != null) {
                    //取出注解的参数
                    int id = annotion.id();
                    if(   id==ordermethodid) {
                        String description = annotion.description();
                        System.out.println("获取到了   方法:" + id + ",描述：" + description);
                        m.invoke(o);  //执行方法
                    }

                }
            }
        }

    }
}
